<?php
/**
 * @package site
 */
require_once '_siteinf.php';
require_once '_sitelang.php';
require_once '_dbconnector.php';
require_once '_constants.php';

/**
 * Session manager and carrier class.
 */
class SiteContext {
	/** @var SiteContext */
	private static $siteContext;

	/** @var SiteLang */
	protected $siteLang;
	/** @var SiteInf */
	protected $siteInf;
	/** @var DBConnector */
	protected $dbConnector;
	/** @var User */
	protected $currentUser;
	/** @var string - Previous page entered. */
	private $previousPage;
	/** @var string - Hash of the last used form */
	private $formHash;
	/** @var array - array of id's of interested in items */
	private $interestedInItem;
	/** @var int - items set in a thumbnail view */
	private $countOfItemsInThumbView;
	/** @var int - items set in a list view */
	private $countOfItemsInListView;
	
	/**
	 * only two values: Constants::REQ_TYPEOFVIEW_LIST and \Constants::REQ_TYPEOFVIEW_VIEW
	 */
	private $preferredView;
	
	/**
	 * Create Site Context.
	 * This is called only when the session is started.
	 */
	private function __construct() {
		/** initialize information about infrastructure */
		$this->siteInf = new SiteInf(); 
		/** initialize information about language */
		$this->siteLang = new SiteLang(SiteLang::mineLangInfo());	
		/** initialize database connector */
		$this->dbConnector = self::createDBConnector();
		
		/** set all other values */
		$this->previousPage = Constants::WEB_DEFAULTPAGE;
		$this->interestedInItem = array();
		$this->preferredView = Constants::REQ_TYPEOFVIEW_LIST;
		$this->countOfItemsInListView = Constants::SETUP_COUNTOFITEMSINLISTVIEW;
		$this->countOfItemsInThumbView = Constants::SETUP_COUNTOFITEMSINTHUMVIEW;
	}
	
	/**
	 * Overridable function for creating different connector to DB.
	 * 
	 * @return DBConnector
	 */
	protected static function createDBConnector() {
		return new DBConnectorMySQL();
	}
	
	/**
	 * Get context of current site / session.
	 *
	 * @return SiteContext
	 */
	static function getSiteContext() {
		if (!is_object(self::$siteContext)) {
			if (is_string(@$_SESSION['info'])) {
				// try to mine it from the session
				$info = unserialize($_SESSION['info']);

				
				if (isset($_GET[Constants::REQ_LANG]) && ($lang = $_GET[Constants::REQ_LANG]) != $info->siteLang->getLang()) {
					$info->siteLang->setLang($lang);
				}
				
				self::$siteContext = $info;
			} else { 
				// create new context
				self::$siteContext = new SiteContext();
			}
			
		}
		return self::$siteContext;
	}
	
	/**
	 * Getter for siteInf.
	 *
	 * @return SiteInf
	 */
	function getSiteInf() {
		return $this->siteInf;
	}
	
	/**
	 * Getter for siteLang.
	 *
	 * @return SiteLang
	 */
	function getSiteLang() {
		return $this->siteLang;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return DBConnector
	 */
	function getDBConnector() {
		return $this->dbConnector;
	}
	
	/**
	 * @return User
	 */
	function getCurrentUser() {
		if (is_null($this->currentUser)) {
			$this->anonymousUserSet();
		}
		return $this->currentUser;
	}
	
	/**
	 * Setter
	 *
	 * @param User $user
	 */
	function setCurrentUser(User $user) {
		if ($user instanceof User) {
			$this->currentUser = $user;
		}
	}
	
	/**
	 * Set anonymous user.
	 */
	function anonymousUserSet() {
		$this->currentUser = new User();
		$this->currentUser->login = Constants::WEB_DEFAULTUSER; // TODO this is really stupid.
	}
	
	/**
	 * Setter
	 *
	 * @param string $page
	 */
	function setPreviousPage(string $page = null) {
		if (is_null($page)) {
			$page = $_SERVER['REQUEST_URI'];
		}
		$this->previousPage = $page;
	}
	
	/**
	 * @return string
	 */
	function getPreviousPage() {
		return $this->previousPage;
	}
	
	/**
	 * Set Hash.
	 *
	 * @param string $hash
	 */
	function setFormHash($hash) {
		$this->formHash = $hash;
	}
	
	/**
	 * @return string
	 */
	function getFormHash() {
		return $this->formHash;
	}
	
	/**
	 * Add an item with id $id into interested intems.
	 *
	 * @param int $id
	 */
	function addInterestedInItem(int $id) {
		$this->interestedInItem[$id] = true;	
	}
	
	/**
	 * @return array - interested in items (array of ids)
	 */
	function getInterestedInItem() {
		return $this->interestedInItem;
	}

	/**
	 * Wheter is or is not an interested in item.
	 *
	 * @param string $id
	 * @return bool
	 */
	function isInterestedInItem(string $id) {
		if (array_key_exists($id, $this->interestedInItem)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Remove interested in item with id $id
	 *
	 * @param int $id
	 */
	function removeInterestedInItem(int $id) {
		if ($this->isInterestedInItem($id)) {
			unset($this->interestedInItem[$id]);
		}
	}
	
	/**
	 * @return string
	 */
	public function getPreferredView() {
		return $this->preferredView == Constants::REQ_TYPEOFVIEW_THUMB ? Constants::REQ_TYPEOFVIEW_THUMB : Constants::REQ_TYPEOFVIEW_LIST;
	}
	
	/**
	 * @param string
	 */
	public function setPreferredView(string $typeOfView) {
		$this->preferredView = $typeOfView == Constants::REQ_TYPEOFVIEW_THUMB ? Constants::REQ_TYPEOFVIEW_THUMB : Constants::REQ_TYPEOFVIEW_LIST;
	}
	
	/**
	 * @return int
	 */
	public function getCountOfItemsInListView() {
		return $this->countOfItemsInListView;
	}
	
	/**
	 * @param int
	 */
	public function setCountOfItemsInListView(int $count) {
		$this->countOfItemsInListView = $count;
	}
	
	/**
	 * @return int
	 */
	public function getCountOfItemsInThumbView() {
		return $this->countOfItemsInThumbView;
	}
	
	/**
	 * @param int
	 */
	public function setCountOfItemsInThumbView(int $count) {
		$this->countOfItemsInThumbView = $count;
	}
}
?>